import {
    ref
} from "vue";
import {
    generateId
} from "../util/todoStorage";

export default function useNewTodo(todosRef) {
    const newTodoRef = ref(""); //新任务标题
    const addTodo = () => {
        //新增任务
        const value = newTodoRef.value && newTodoRef.value.trim();
        if (!value) {
            return;
        }
        // console.log(newTodoRef.value);

        //生成任务对象，加入任务列表
        const todo = {
            title: value,
            completed: false,
            id: generateId(),
        }
        todosRef.value.push(todo);
        newTodoRef.value = "";
    }

    return {
        newTodoRef,
        addTodo,
    }
}